package br.com.pd.messaging;

import java.util.ArrayList;
import java.util.List;

import br.com.util.PropertiesLoader;


public class ProducerRequestHandle implements RequestHandleIF, Listener, Subject {

	private RequestSender sender;
	private RequestReceiver receiver;
	private List<Listener> listeners;
	
	public ProducerRequestHandle() {
		this.sender = new RequestSender();
		this.receiver = new RequestReceiver(Integer.parseInt(PropertiesLoader.getInstance().getPropertie(PropertiesLoader.PORT_FACTORY_RECEIVER)));
		this.receiver.addObserver(this);
		this.listeners = new ArrayList<Listener>();
		this.sender.start();
		this.receiver.start();
	}

	@Override
	public String receive() {
		return receiver.receive();
	}

	@Override
	public void send(String b, String location, int port) {
		this.sender.send(b, location, port);
	}


	@Override
	public void update() {
		for (Listener l : this.listeners) {
			l.update();
		}
	}

	
	@Override
	public void addObserver(Listener o) {
		this.listeners.add(o);
	}

	@Override
	public void removeObserver(Listener o) {
		this.listeners.remove(o);
	}
	
	
	
	
}
